var config = require("config");
cc.Class({
    extends: cc.Component,

    properties: {
    },
    onLoad (){
    },
    init () {
        // var t = cc.sys.localStorage.getItem("bgmVolume");
        // cc.sys.localStorage.setItem("bgmVolume",this.bgmVolume);
        /*游戏存档
        1.游戏角色信息（攻击 血量 共5个）
        2.当前选中角色
        3.关卡等级（和总距离挂钩）(每次最多距离)
        4.金币信息 
        5.宠物信息
        6.游戏次数
        7.自己的排名
        */
        var oldPlayer = cc.sys.localStorage.getItem("oldPlayer2");
        // console.log(">>>>>>初始化游戏数据",typeof(oldPlayer),oldPlayer);
        if(oldPlayer == "false" || !oldPlayer){
            cc.sys.localStorage.setItem("oldPlayer2",true);
            console.log("第一次进入游戏");
            cc.sys.localStorage.setItem("PlayerGold",0);
            cc.sys.localStorage.setItem("AllRecordMi",0);
            cc.sys.localStorage.setItem("MaxRecordMi",0);
            cc.sys.localStorage.setItem("CurHeroIndex",1);
            cc.sys.localStorage.setItem("CurPetIndex",-1);
            cc.sys.localStorage.setItem("GameNumber",0);
            cc.sys.localStorage.setItem("MyRank",0);
            cc.sys.localStorage.setItem("bgmVolume",1);
            cc.sys.localStorage.setItem("sfxVolume",1);
            //初始化角色信息
            for(var i=1;i<=5;i++){
                this.initHeroInfo(i,1,1,true);
            }
            //初始化宠物信息
            for(var i=1;i<=6;i++){
                this.initPetInfo(i,1,false);
            }
        }
        // cc.sys.localStorage.setItem("oldPlayer2",false);

        config.PlayerGold = this.getGold();
        config.AllRecordMi = this.getAllRecordMi();
        config.MaxRecordMi = this.getMaxRecordMi();
        console.log("MaxRecordMi",config.MaxRecordMi);
        config.userRank = this.getMyRank();; //用户排行

        config.CurHeroIndex = this.getCurHeroIndex();//当前英雄编号
        config.CurPetIndex = this.getCurPetIndex();//当前宠物编号 -1 (没有)

        //读取角色存档
        for(var i=1;i<=5;i++){
            var heroInfo = this.getHeroInfo(i);
            // console.log("heroInfo:",heroInfo);
            config.heroInfo["hero"+i].hit = heroInfo.hit;
            config.heroInfo["hero"+i].hp = heroInfo.hp;
            config.heroInfo["hero"+i].locked = heroInfo.locked;
        }
        //读取宠物存档
        for(var i=1;i<=6;i++){
            var petInfo = this.getPetInfo(i);
            console.log("petInfo:",petInfo);
            config.petInfo["pet"+i].lv = petInfo.lv;
            config.petInfo["pet"+i].exist = petInfo.exist;
            config.petInfo["pet"+i].hit = 50*i*petInfo.lv;
        }

        //宠物攻击力
        if(config.CurPetIndex >= 0){
            var index = config.CurPetIndex + 1;
            config.PetBulletHpDrop = config.petInfo["pet"+index].hit;
            console.log("config.PetBulletHpDrop==",config.PetBulletHpDrop);
        }
    },
    //游戏次数
    saveGameNumber(index){
        cc.sys.localStorage.setItem("GameNumber",index);
    },
    getGameNumber(){
        var index = cc.sys.localStorage.getItem("GameNumber");
        if(!index || typeof(index)=="undefined")
            index=0;
        return parseInt(index);
    },
    //当前出战英雄编号
    saveCurHeroIndex(index){
        cc.sys.localStorage.setItem("CurHeroIndex",index);
    },
    getCurHeroIndex(){
        var index = cc.sys.localStorage.getItem("CurHeroIndex");
        if(!index || typeof(index)=="undefined")
            index=1;
        return parseInt(index);
    },
    //当前出战宠物编号
    saveCurPetIndex(index){
        cc.sys.localStorage.setItem("CurPetIndex",index);
    },
    getCurPetIndex(){
        var index = cc.sys.localStorage.getItem("CurPetIndex");
        if((!index && index != 0) || typeof(index)=="undefined")
            index=-1;
        return parseInt(index);
    },
    initHeroInfo(index,hit,hp,locked){
        var heroName = "hero"+index;
        var heroinfo = {}
        heroinfo.hit = hit;
        heroinfo.hp = hp;
        heroinfo.locked = locked;
        if(index == 1){
            heroinfo.locked = false;
        }
        cc.sys.localStorage.setItem(heroName, JSON.stringify(heroinfo));
    },
    saveHeroInfo(index){
        var heroName = "hero"+index;
        var heroinfo = {}
        heroinfo.hit = config.heroInfo[heroName].hit;
        heroinfo.hp = config.heroInfo[heroName].hp;
        heroinfo.locked = config.heroInfo[heroName].locked;
        if(index == 1){
            heroinfo.locked = false;
        }
        cc.sys.localStorage.setItem(heroName, JSON.stringify(heroinfo));
    },
    getHeroInfo(index){
        var heroName = "hero"+index;
        var heroData = cc.sys.localStorage.getItem(heroName);
        return JSON.parse(heroData);
    },
    //宠物信息
    initPetInfo(index,lv,exist){
        var Name = "pet"+index;
        var info = {}
        info.lv = lv;
        info.exist = exist;
        cc.sys.localStorage.setItem(Name, JSON.stringify(info));
    },
    savePetInfo(index){
        var Name = "pet"+index;
        var info = {}
        info.lv = config.petInfo[Name].lv;
        info.exist = config.petInfo[Name].exist;
        cc.sys.localStorage.setItem(Name, JSON.stringify(info));
    },
    getPetInfo(index){
        var Name = "pet"+index;
        var Data = cc.sys.localStorage.getItem(Name);
        return JSON.parse(Data);
    },
    //金币信息
    saveGold(gold){
        cc.sys.localStorage.setItem("PlayerGold",gold);
    },
    getGold(){
        var gold = cc.sys.localStorage.getItem("PlayerGold");
        if(!gold)
            gold=0;
        return parseInt(gold);
    },
    //总里程记录
    saveAllRecordMi(record){
        cc.sys.localStorage.setItem("AllRecordMi",record);
    },
    getAllRecordMi(){
        var record = cc.sys.localStorage.getItem("AllRecordMi");
        if(!record || typeof(record)=="undefined")
            record=0;
        return parseInt(record);
    },
    //最大里程记录
    saveMaxRecordMi(record){
        cc.sys.localStorage.setItem("MaxRecordMi",record);
    },
    getMaxRecordMi(){
        var record = cc.sys.localStorage.getItem("MaxRecordMi");
        if(!record || typeof(record)=="undefined")
            record=0;
        return parseInt(record);
    },
    //自己排行
    saveMyRank(rank){
        cc.sys.localStorage.setItem("MyRank",rank);
    },
    getMyRank(){
        var rank = cc.sys.localStorage.getItem("MyRank");
        if(!rank || typeof(rank)=="undefined")
            rank=0;
        return parseInt(rank);
    },
});
